package com.jinzecloud.reportapproval.service.impl;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopColumnTableRenderPolicy;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.jinzecloud.reportapproval.bean.DeepStabilityRecord;
import com.jinzecloud.reportapproval.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.jinzecloud.reportapproval.mapper.ExperimenttalInvestigationRecordMapper;
import com.jinzecloud.reportapproval.bean.ExperimenttalInvestigationRecord;
import com.jinzecloud.reportapproval.service.ExperimenttalInvestigationRecordService;

import javax.servlet.http.HttpServletRequest;


@Service("experimenttalInvestigationRecordService")
public class ExperimenttalInvestigationRecordServiceImpl extends ServiceImpl<ExperimenttalInvestigationRecordMapper, ExperimenttalInvestigationRecord> implements ExperimenttalInvestigationRecordService {
    @Autowired
    private FileUploadUtil fileUploadUtil;
    @Autowired
    private HttpServletRequest request;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ExperimenttalInvestigationRecord> page = this.page(
                new Query<ExperimenttalInvestigationRecord>().getPage(params),
                new QueryWrapper<ExperimenttalInvestigationRecord>().like("test_location",params.get("key"))
        );

        return new PageUtils(page);
    }

    @Override
    public String createPdf(Integer id) {
        ExperimenttalInvestigationRecord experimenttalInvestigationRecord=getById(id);
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(experimenttalInvestigationRecord));
        // 转换 gaugingPoint 数据为可循环的格式

        JSONObject stubbleHeightData = data.getJSONObject("stubbleHeight");
        JSONObject HeightData = data.getJSONObject("height");

        // 变量用于跟踪当前的索引
        int index = 1;
        int tt=1;
        int fontSize = 6;
        // 提取所有测点的值
        for (int i=1;i<=3;i++) {
            JSONArray values = stubbleHeightData.getJSONArray("测点"+String.valueOf(i));
            JSONArray hvalues = HeightData.getJSONArray("测点"+String.valueOf(i));
            for (int j = 0; j < values.size(); j++) {
                data.put("cd" + index++, values.getString(j));
                data.put("cd" + (index - 1) + "_fontSize", fontSize); // 设置字体大小
            }
            for (int j = 0; j < hvalues.size(); j++) {
                data.put("h" + tt++, hvalues.getString(j));
                data.put("h" + (tt - 1) + "_fontSize", fontSize); // 设置字体大小
            }
        }
        File tempFile = WordUtil.getWordFile("template/experimenttal.docx", data);

        if (tempFile == null) return null;
        String fileName = "试验田调查记录表";
        String uploadDirectory = fileUploadUtil.getUploadFilePath("temp");
        String filePath = uploadDirectory + experimenttalInvestigationRecord.getProjectNum() + fileName;
        String savePath = RequestUtils.getBasePath(request) + "upload/temp/" + experimenttalInvestigationRecord.getProjectNum() +fileName;
        FileUtil.copyFile(tempFile, new File(filePath + ".docx"), StandardCopyOption.REPLACE_EXISTING);
        PDFUtil.WordToPdf(tempFile, new File(filePath + ".pdf"));

        update(new LambdaUpdateWrapper<ExperimenttalInvestigationRecord>().set(ExperimenttalInvestigationRecord::getWordPath, savePath + ".docx").set(ExperimenttalInvestigationRecord::getPdfPath, savePath + ".pdf").eq(ExperimenttalInvestigationRecord::getId, id));
        return RequestUtils.getBasePath(request) + "upload/temp/" + experimenttalInvestigationRecord.getProjectNum() + fileName + ".pdf" ;
    }

    @Override
    public JSONObject experimenttalInvestigationRecordJson(String projectUuid) {
        ExperimenttalInvestigationRecord experimenttalInvestigationRecord=getOne(new QueryWrapper<ExperimenttalInvestigationRecord>().eq("project_num", projectUuid));
        if(experimenttalInvestigationRecord == null) return null;
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(experimenttalInvestigationRecord));
        // 转换 gaugingPoint 数据为可循环的格式

        JSONObject stubbleHeightData = data.getJSONObject("stubbleHeight");
        JSONObject HeightData = data.getJSONObject("height");

        // 变量用于跟踪当前的索引
        int index = 1;
        int tt=1;
        int fontSize = 6;
        // 提取所有测点的值
        for (int i=1;i<=3;i++) {
            JSONArray values = stubbleHeightData.getJSONArray("测点"+String.valueOf(i));
            JSONArray hvalues = HeightData.getJSONArray("测点"+String.valueOf(i));
            for (int j = 0; j < values.size(); j++) {
                data.put("cd" + index++, values.getString(j));
                data.put("cd" + (index - 1) + "_fontSize", fontSize); // 设置字体大小
            }
            for (int j = 0; j < hvalues.size(); j++) {
                data.put("h" + tt++, hvalues.getString(j));
                data.put("h" + (tt - 1) + "_fontSize", fontSize); // 设置字体大小
            }
        }

        return data;
    }

    @Override
    public String createWord(Integer id) {
        ExperimenttalInvestigationRecord experimenttalInvestigationRecord=getById(id);
        JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(experimenttalInvestigationRecord));
        // 转换 gaugingPoint 数据为可循环的格式

        JSONObject stubbleHeightData = data.getJSONObject("stubbleHeight");
        JSONObject HeightData = data.getJSONObject("height");

        // 变量用于跟踪当前的索引
        int index = 1;
        int tt=1;
        int fontSize = 6;
        // 提取所有测点的值
        for (int i=1;i<=3;i++) {
            JSONArray values = stubbleHeightData.getJSONArray("测点"+String.valueOf(i));
            JSONArray hvalues = HeightData.getJSONArray("测点"+String.valueOf(i));
            for (int j = 0; j < values.size(); j++) {
                data.put("cd" + index++, values.getString(j));
                data.put("cd" + (index - 1) + "_fontSize", fontSize); // 设置字体大小
            }
            for (int j = 0; j < hvalues.size(); j++) {
                data.put("h" + tt++, hvalues.getString(j));
                data.put("h" + (tt - 1) + "_fontSize", fontSize); // 设置字体大小
            }
        }
        File tempFile = WordUtil.getWordFile("template/experimenttal.docx", data);

        if (tempFile == null) return null;
        String fileName = "试验田调查记录表";
        String uploadDirectory = fileUploadUtil.getUploadFilePath("temp");
        String filePath = uploadDirectory + experimenttalInvestigationRecord.getProjectNum() + fileName;
        String savePath = RequestUtils.getBasePath(request) + "upload/temp/" + experimenttalInvestigationRecord.getProjectNum() +fileName;
        FileUtil.copyFile(tempFile, new File(filePath + ".docx"), StandardCopyOption.REPLACE_EXISTING);
        PDFUtil.WordToPdf(tempFile, new File(filePath + ".pdf"));

        update(new LambdaUpdateWrapper<ExperimenttalInvestigationRecord>().set(ExperimenttalInvestigationRecord::getWordPath, savePath + ".docx").set(ExperimenttalInvestigationRecord::getPdfPath, savePath + ".pdf").eq(ExperimenttalInvestigationRecord::getId, id));
        return RequestUtils.getBasePath(request) + "upload/temp/" + experimenttalInvestigationRecord.getProjectNum() + fileName + ".docx" ;
    }
}